package com.auxiliary;

import java.util.ArrayList;
import java.util.List;

import com.myidis.response.BBCalcRes;

public class BBForPeakValley {

	private int halfPeriod;
	private int onePeriod;
	private int springDay;
	
	private String start;
	
	private List<Double> analysis;
	
	private SeasonAdjuster adjuster;
	
	public BBForPeakValley() {
		this.halfPeriod = 6;
		this.onePeriod = 15;
		this.start = null;
		this.analysis = new ArrayList<Double>();
	}
	
	public BBForPeakValley(TimeSequence analysisSequence) {
		this.halfPeriod = 6;
		this.onePeriod = 15;
		this.start = analysisSequence.getStartDate();
		this.analysis = analysisSequence.getValueList();
		
		this.adjuster = new SeasonAdjuster(analysisSequence, analysisSequence.getFrequency(), 1);
	}
	
	public BBForPeakValley(TimeSequence analysisSequence, int halfPeriod, int onePeriod) {
		this.halfPeriod = halfPeriod;
		this.onePeriod = onePeriod;
		this.start = analysisSequence.getStartDate();
		this.analysis = analysisSequence.getValueList();
		
		this.adjuster = new SeasonAdjuster(analysisSequence, analysisSequence.getFrequency(), 1);
	}

	public BBForPeakValley(List<Double> analysisSeries, String start, int springDay)
	{
		this.analysis = analysisSeries;
		this.start = start;
		this.springDay = springDay;
		this.halfPeriod = 6;
		this.onePeriod = 15;
	}
	
	public BBCalcRes calc() {
		int result[][] = calcExtremumByBB();
		BBCalcRes bbRes = new BBCalcRes();
		
		BasicCalculator bc = new BasicCalculator();
		
		for(int i = 0; i < result.length; i++) {
			if(result[i][1] == 0)
				bbRes.addValley(bc.addMonth(start, result[i][0]));
			else
				bbRes.addPeak(bc.addMonth(start, result[i][0]));
		}
		
		return bbRes;
	}
	
	//执行bb算法
	public int[][] calcExtremumByBB() {
		int MCD = adjuster.getMCD();
		
		List<Double> list = new ArrayList<Double>();
	
		BasicCalculator bc = new BasicCalculator();
		
		//修正特异值
		list = bc.removeOutlier(analysis);
		List<Double> temp = bc.movingAvg(12, list);
		int PVSeries[][] = bc.findConstriantPV(bc.findPV(temp), halfPeriod, onePeriod);
		
		//在 spencer 曲线上确定转折点
		temp = bc.DoSpencer(list);
		PVSeries = bc.adjustPV(PVSeries, MCD, temp);
		
		//进行 MCD 项移动平均，在MCD曲线上确定转折点
		temp = bc.movingAvg(MCD, analysis);
		PVSeries = bc.adjustPV(PVSeries, MCD, temp);
		
		//确定原序列的转折点
		PVSeries = bc.findConstriantPV(bc.adjustPV(PVSeries, MCD, analysis), halfPeriod, onePeriod);
		return PVSeries;
	}
}
